<?php

namespace App\AgentAdmin\Metrics\Orders;

use Dcat\Admin\Admin;
use Dcat\Admin\Widgets\Metrics\Donut;
use Illuminate\Http\Request;
use Carbon\Carbon;
use App\Models\Order;

class DayOrderMoneyCount extends Donut
{
    protected $labels = ['今日', '昨天','前天'];

    /**
     * 初始化卡片内容
     */
    protected function init()
    {
        parent::init();

        $color = Admin::color();
        $colors = [$color->primary(), $color->success(),$color->warning()];

        $this->title('最近3天已支付订单金额');
        // $this->dropdown([
        //     '1' => '今日',
        //     '7' => '最近 7 天',
        //     '10' => '最近 10 天',
        //     '30' => '最近 30 天',
        //     '100' => '最近 100 天',
        // ]);
        // $this->subTitle('Last 30 days');
        $this->chartLabels($this->labels);
        // 设置图表颜色
        $this->chartColors($colors);
    }

    /**
     * 处理请求
     *
     * @param Request $request
     *
     * @return mixed|void
     */
    public function handle(Request $request)
    {
        $date = Carbon::today();

        $today = Order::paid()->where(['agent_id'=>(Admin::user()->id),'is_deduct'=>0])->whereDate('paid_at', '>=', $date)->sum('agent_price');
        $yesterday = Order::paid()->where(['agent_id'=>(Admin::user()->id),'is_deduct'=>0])->whereDate('paid_at', '>=', Carbon::today()->addDays(-1))->whereDate('paid_at', '<', Carbon::today())->sum('agent_price');
        $before_yesterday = Order::paid()->where(['agent_id'=>(Admin::user()->id),'is_deduct'=>0])->whereDate('paid_at', '>=', Carbon::today()->addDays(-2))->whereDate('paid_at', '<', Carbon::today()->addDays(-1))->sum('agent_price');
        $this->withContent($today, $yesterday,$before_yesterday);

        // 图表数据
        $this->withChart([$today, $yesterday,$before_yesterday]);
    }

    /**
     * 设置图表数据.
     *
     * @param array $data
     *
     * @return $this
     */
    public function withChart(array $data)
    {
        return $this->chart([
            'series' => $data
        ]);
    }

    /**
     * 设置卡片头部内容.
     *
     * @param mixed $desktop
     * @param mixed $mobile
     *
     * @return $this
     */
    protected function withContent($today, $yesterday,$before_yesterday)
    {
        $yellow = Admin::color()->warning();
        $green = Admin::color()->success();

        $style = 'margin-bottom: 8px';
        $labelWidth = 120;

        return $this->content(
            <<<HTML
<div class="d-flex pl-1 pr-1 pt-1" style="{$style}">
    <div style="width: {$labelWidth}px">
        <i class="fa fa-circle text-primary"></i> {$this->labels[0]}
    </div>
    <div>￥{$today}</div>
</div>
<div class="d-flex pl-1 pr-1" style="{$style}">
    <div style="width: {$labelWidth}px">
        <i class="fa fa-circle" style="color: $green"></i> {$this->labels[1]}
    </div>
    <div>￥{$yesterday}</div>
</div>
<div class="d-flex pl-1 pr-1" style="{$style}">
    <div style="width: {$labelWidth}px">
        <i class="fa fa-circle" style="color: $yellow"></i> {$this->labels[2]}
    </div>
    <div>￥{$before_yesterday}</div>
</div>
HTML
        );
    }
}
